#!/bin/bash

set -e

if [ "`whoami`" != "root" ]; then
	echo "User must be root."
	exit 0
fi

apt-get install genisoimage p7zip-full wget squashfs-tools  -y --force-yes

if [ "$?" != "0" ]; then
	echo "Commands required: mkisofs, 7z, wget."
	exit 0
fi

if [ "`uname -a | grep i.86`" == "" ]; then
	echo "Architecture required: i386|i486|i586|i686."
	exit 0
fi

if [ "`which mksquashfs`" == "" ]; then
	cp tools/bin/* /usr/local/bin
fi

IMAGE=$(basename `pwd`)

DOWNLOAD="ubuntu-core-14.04.3-core-i386.tar.gz"
IMAGE="ubuntu-trusty"


umount squashfs-root/* 2>/dev/null || true
rm -rf squashfs-root boot
mkdir squashfs-root boot
cd squashfs-root
tar xzvf ../$DOWNLOAD


rm -f etc/resolv.conf
ln -s ../run/resolvconf/resolv.conf etc/
cp /run/resolvconf/resolv.conf run/resolvconf/

echo "deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse" > etc/apt/recommends.list
echo "deb http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse" >> etc/apt/recommends.list
echo 'APT::Install-Recommends "false";' > etc/apt/apt.conf
rm -f recommends.list etc/apt/sources.list
ln -s recommends.list etc/apt/sources.list
sed -i '$i\rm -f /etc/apt/sources.list' etc/rc.local
sed -i '$i\ln -s recommends.list /etc/apt/sources.list' etc/rc.local
sed -i '$i\rfkill block bluetooth' etc/rc.local

mkdir -p etc/lightdm/lightdm.conf.d
echo '[SeatDefaults]' > etc/lightdm/lightdm.conf.d/90-default.conf
echo 'autologin-user=ubuntu' >> etc/lightdm/lightdm.conf.d/90-default.conf
echo 'autologin-user-timeout=0' >> etc/lightdm/lightdm.conf.d/90-default.conf
echo 'autologin-guest=false' >> etc/lightdm/lightdm.conf.d/90-default.conf
echo '. /etc/bash_completion' >> etc/bash.bashrc

cp ../tools/bin/* usr/local/bin

chroot . chmod a+s /bin/ping
chroot . apt-get purge vim-common -y --force-yes
mount -o bind ../boot boot/
mount -o bind /sys sys/
mount -o bind /dev dev/
mount -o bind /proc proc/
mount -o bind /dev/pts dev/pts/
chroot . apt-get update
chroot . apt-get upgrade -y --force-yes



DEBIAN_FRONTEND=noninteractive chroot . apt-get install grub-pc os-prober -y --force-yes



DEBIAN_FRONTEND=noninteractive chroot . apt-get install linux-image-generic xinit xserver-xorg casper lightdm lightdm-gtk-greeter bash-completion nano ttf-ubuntu-font-family ttf-wqy-microhei   fcitx-ui-classic fcitx-frontend-gtk2 fcitx-config-gtk lzma network-manager-gnome network-manager-pptp-gnome gnome-keyring rfkill genisoimage xz-utils openssh-client sshfs  libcurl3 unrar   ubuntu-mono pulseaudio pavucontrol alsa-base alsa-utils language-pack-gnome-zh-hans software-properties-common cpp desktop-file-utils gconf-service gconf-service-backend gconf2 gconf2-common gksu gvfs  gvfs-bin gvfs-common gvfs-daemons gvfs-libs libasn1-8-heimdal  libasound2-plugins libasyncns0 libatasmart4 libcloog-isl4  libexempi3 libexif12 libfftw3-single3 libflac8 libgail18 libgconf-2-4  libgcrypt20 libgksu2-0 libgmp10 libgnome-keyring-common libgnome-keyring0  libgomp1 libgssapi3-heimdal libgtk2.0-bin  libgtop2-common libgudev-1.0-0 libhcrypto4-heimdal libheimbase1-heimdal  libheimntlm0-heimdal libhogweed2 libhx509-5-heimdal libimobiledevice4  libisl10 libjack-jackd2-0 libkrb5-26-heimdal libldap-2.4-2  libmpc3 libmpfr4 libnettle4 libpango1.0-0 libpangox-1.0-0  libpulse-mainloop-glib0 libpulse0 libpython-stdlib  libpython2.7-minimal libpython2.7-stdlib libroken18-heimdal libsamplerate0  libsasl2-2 libsasl2-modules-db libsndfile1 libspeexdsp1 libudisks2-0  libunique-1.0-0 libupower-glib1 libusb-1.0-0 libusbmuxd2 libvorbisenc2  libvte-common libvte9 libwind0-heimdal libwnck-common libwnck22 libwrap0  libxcb-shape0 libxres1 libxss1 libxtst6 libxxf86dga1  menu-xdg parted psmisc pulseaudio python python-minimal  python2.7 python2.7-minimal python3-chardet python3-debian  python3-pkg-resources python3-six udisks2 upower x11-utils  x11-xserver-utils  libarchive13 libcupsfilters1 libcupsimage2 libdjvulibre-text libdjvulibre21 libgs9 libgs9-common libgtksourceview2.0-0 libgtksourceview2.0-common libgxps2 libijs-0.35 libjbig2dec0 libkpathsea6 liblzo2-2 libpaper1 libpoppler-glib8 libpython2.7 libspectre1  p7zip-full gtk2-engines gtk2-engines-pixbuf gtk2-engines-murrine poppler-data gdebi squashfs-tools  -y --force-yes

DEBIAN_FRONTEND=noninteractive chroot . apt-get install  fcitx-googlepinyin   gparted  ubiquity-frontend-gtk -y --force-yes

cp ../tools/libav-tools-fixed.deb .
chroot . gdebi -n libav-tools-fixed.deb
rm -f libav-tools-fixed.deb



#ln -s avconv usr/bin/ffmpeg
#ln -s avplay usr/bin/ffplay

############################################
#	Mate Desktop Environment Install

chroot . bash -c 'echo "`cat /etc/xdg/autostart/nm-applet.desktop | grep -v GNOME3`" > /etc/xdg/autostart/nm-applet-mate.desktop'
# vivid
chroot . bash -c 'echo "deb http://mirrors.163.com/ubuntu/ utopic main restricted universe multiverse" > /etc/apt/sources.list.d/extra.list'
chroot . bash -c 'echo "deb http://mirrors.163.com/ubuntu/ utopic-updates main restricted universe multiverse" >> /etc/apt/sources.list.d/extra.list'

chroot . apt-get update
chroot . apt-get install mate-desktop-environment-core atril pluma engrampa eom mate-media mate-power-manager mate-screensaver mate-utils -y  --force-yes

chroot . rm -f /etc/apt/sources.list.d/extra.list
chroot . rm -r /usr/share/icons/mate /usr/share/icons/menta
chroot . rm -r /usr/share/backgrounds/mate /usr/share/backgrounds/cosmos

cd usr/share/themes/ && rm -r `ls | grep -v Default`
cd ../../.. && cp -r ../tools/themes/* usr/share/themes/

cp ../tools/mate-settings/*-mate.desktop etc/xdg/autostart/
cp ../tools/mate-settings/mate-*.desktop usr/share/applications
cp ../tools/mate-settings/warty-final-ubuntu.png usr/share/backgrounds/
cp ../tools/mate-settings/ubuntu-mate.layout usr/share/mate-panel/layouts/
cp ../tools/mate-settings/ubuntu-mate.gschema.override usr/share/glib-2.0/schemas
cp ../tools/mate-settings/start-here.svg usr/share/icons/ubuntu-mono-dark/apps/22/

echo 'allow-guest=false' >> usr/share/lightdm/lightdm.conf.d/60-lightdm-gtk-greeter.conf
chroot . bash -c "cd /usr/share/glib-2.0/schemas && glib-compile-schemas ."

ln -s pluma usr/bin/gedit
ln -s caja usr/bin/nautilus
ln -s mate-terminal usr/bin/gnome-terminal
ln -s eom usr/bin/eog
ln -s engrampa usr/bin/file-roller

mv -f usr/share/locale-langpack/zh_CN/LC_MESSAGES/* usr/share/locale/zh_CN/LC_MESSAGES/
rm -rf usr/share/locale-langpack/

cd usr/share/applications

sed -i 's/AudioVideo\;//' mate-volume-control.desktop
sed -i 's/gnome-mime-application-x-deb/gdebi/' gdebi.desktop
sed -i 's/GenericName\[zh_CN\]/Name\[zh_CN\]/' engrampa.desktop
sed -i 's/GenericName\[zh_CN\]/Name\[zh_CN\]/' atril.desktop
sed -i 's/GenericName\[zh_CN\]/Name\[zh_CN\]/' mate-terminal.desktop
sed -i 's/System\;//' mate-terminal.desktop
sed -i 's/GenericName\[zh_CN\]/Name\[zh_CN\]/' caja-browser.desktop
sed -i 's/System\;//' caja-browser.desktop
sed -i 's/MATE\ 之眼//' eom.desktop
sed -i 's/NoDisplay\=true//' python*.desktop
sed -i 's/multimedia-volume-control/audio-volume-medium/' pavucontrol.desktop
sed -i 's/AudioVideo\;Audio/Settings/' pavucontrol.desktop

TEXT=`cat ubiquity.desktop | grep -v 'Exec'`
echo "$TEXT" > ubiquity.desktop
echo "NoDisplay=true" >> ubiquity.desktop
echo 'Exec=sh -c "sudo umount -l /isodevice 2>/dev/null ; ubiquity gtk_ui"' >> ubiquity.desktop

cd ../../lib/ubiquity/localechooser
TEXT=`grep -E "zh_CN.UTF-8|en_US.UTF-8" languagelist`
echo "$TEXT" > languagelist
TEXT=`zgrep -E "zh_CN|English" languagelist.data.gz`
echo "$TEXT" > languagelist.data
gzip languagelist.data -f
cd ../ubiquity
sed -i "s/size\ \=\ 5/return\ 2/" misc.py

cd ../../../../etc/gnome
sed -i 's/rhythmbox.desktop/mate-player.desktop/' defaults.list
sed -i 's/totem.desktop/mate-player.desktop/' defaults.list

cd ../xdg/autostart
rm nm-applet.desktop pulseaudio-kde.desktop gsettings-data-convert.desktop polkit-gnome-authentication-agent-1.desktop
cd ../../..

echo 'export GTK_IM_MODULE="xim"' > etc/X11/Xsession.d/95fcitx_start
echo 'export XMODIFIERS="@im=fcitx"' >> etc/X11/Xsession.d/95fcitx_start

rm etc/init/tty3.conf etc/init/tty4.conf etc/init/tty5.conf

############################################

chroot . apt-get clean
chroot . xsession-config
umount boot/
umount dev/
umount sys/
umount proc/
umount dev/pts/
cd ../boot
cp config-* ../squashfs-root/boot
cp ../tools/memdisk ../squashfs-root/boot
cp vmlinuz-* ../cdimage/casper/vmlinuz
7z x initrd.img-*-generic
rm initrd.img-*-generic
7z x initrd.img-*
cp -r lib/modules/*-generic ../initrd.d/lib/modules
cp -r lib/firmware/*-generic ../initrd.d/lib/firmware
cd ../initrd.d
find . | cpio -o -H newc > ../initrd
cd ..
echo -n 'Compressing initramfs .. ' && lzma -z initrd
mv initrd.lzma cdimage/casper/initrd.lz
cd squashfs-root
rm -rf ../cdimage/casper/filesystem.squashfs
mksquashfs * ../cdimage/casper/filesystem.squashfs
cd ..
mkisofs -o $IMAGE.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -r -V "$IMAGE" -cache-inodes -J -l cdimage/



